﻿using System;
using System.Data.OleDb;
using System.Text;
using System.Web.Security;
using Framework;

namespace Enterprise
{
    public class MemberService : BaseService
    {
        public int Add(string name, string nickname, string account, string password, string email, string weiBo, string address, string country, string city, string province, string county, string town, string qq, string web, string mobile, string telephone, string birthday)
        {
            string code = string.Empty;
            string ip = RequestExtension.GetIp();
            var sbSql = new StringBuilder();
            sbSql.Append("insert into TbMember(");
            sbSql.Append("CoCode,CoName,CoNickname,CoAccount,CoPassword,CoIntegral,CoDate,CoIp,CoEmail,CoWeiBo,CoAddress,CoCountry,CoCity,CoProvince,CoCounty,CoTown,CoQq,CoWeb,CoMobile,CoTelephone,CoBirthday,CoLunarMonths,CoLunarYear,CoLunarDay,CoIsLocking,CoActivationId,CoIsActivation");
            sbSql.Append(") values(");
            sbSql.Append("@CoCode,@CoName,@CoNickname,@CoAccount,@CoPassword,@CoIntegral,@CoDate,@CoIp,@CoEmail,@CoWeiBo,@CoAddress,@CoCountry,@CoCity,@CoProvince,@CoCounty,@CoTown,@CoQq,@CoWeb,@CoMobile,@CoTelephone,@CoBirthday,@CoLunarMonths,@CoLunarYear,@CoLunarDay,@CoIsLocking,@CoActivationId,@CoIsActivation");
            sbSql.Append(")");
            var parameters = new[]
                                {
                                    new OleDbParameter("@CoCode", OleDbType.VarChar, 100),
                                    new OleDbParameter("@CoName", OleDbType.VarChar, 50),
                                    new OleDbParameter("@CoNickname", OleDbType.VarChar, 50),
                                    new OleDbParameter("@CoAccount", OleDbType.VarChar, 50),
                                    new OleDbParameter("@CoPassword", OleDbType.VarChar, 50),
                                    new OleDbParameter("@CoIntegral", OleDbType.Date),
                                    new OleDbParameter("@CoDate",OleDbType.Date),
                                    new OleDbParameter("@CoIp", OleDbType.VarChar, 50),
                                    new OleDbParameter("@CoEmail", OleDbType.VarChar, 80),
                                    new OleDbParameter("@CoWeiBo", OleDbType.VarChar, 255),
                                    new OleDbParameter("@CoAddress", OleDbType.VarChar, 255),
                                    new OleDbParameter("@CoCountry", OleDbType.VarChar, 100),
                                    new OleDbParameter("@CoCity", OleDbType.VarChar, 100),
                                    new OleDbParameter("@CoTown", OleDbType.VarChar, 100),
                                    new OleDbParameter("@CoQq", OleDbType.VarChar, 100),
                                    new OleDbParameter("@CoWeb", OleDbType.VarChar, 255),
                                    new OleDbParameter("@CoMobile", OleDbType.VarChar, 30),
                                    new OleDbParameter("@CoTelephone", OleDbType.VarChar, 30),
                                    new OleDbParameter("@CoBirthday", OleDbType.Date),
                                    new OleDbParameter("@CoLunarMonths", OleDbType.Integer),
                                    new OleDbParameter("@CoLunarYear", OleDbType.Integer),
                                    new OleDbParameter("@CoLunarDay", OleDbType.Integer),
                                    new OleDbParameter("@CoIsLocking",OleDbType.Boolean),
                                    new OleDbParameter("@CoActivationId", OleDbType.VarChar, 100),
                                    new OleDbParameter("@CoIsActivation",OleDbType.Boolean) ,
                                    new OleDbParameter("@CoActivationExpired", OleDbType.Date)
                                };
            parameters[0].Value = code;
            parameters[1].Value = name;
            parameters[2].Value = nickname;
            parameters[3].Value = account;
            parameters[4].Value = password;
            parameters[5].Value = 0;
            parameters[6].Value = DateTime.Now;
            parameters[7].Value = ip;
            parameters[8].Value = email;
            parameters[9].Value = weiBo;
            parameters[10].Value = address;
            parameters[11].Value = country;
            parameters[12].Value = city;
            parameters[13].Value = town;
            parameters[14].Value = qq;
            parameters[15].Value = web;
            parameters[16].Value = mobile;
            parameters[17].Value = telephone;
            parameters[18].Value = birthday;
            parameters[19].Value = "";
            parameters[20].Value = "";
            parameters[21].Value = "";
            parameters[22].Value = GenerateActivationCode(email);
            parameters[23].Value = false;
            parameters[24].Value = DateTime.Now;
            return DbHelperOleDb.Add(sbSql.ToString(), parameters);
        }
        /// <summary>
        /// 激活账号
        /// </summary>
        /// <param name="memberId">会员ID</param>
        /// <param name="account"></param>
        /// <param name="activationId"></param>
        /// <returns></returns>
        public bool Activation(string memberId, string account, string activationId)
        {
            var sql = new StringBuilder();
            sql.Append("update TbMember set CoActivationId=@CoActivationId  where CoMemberId=@CoMemberId and CoIsActivation=@CoIsActivation and CoActivationId=@CoActivationId;");
            var parameter = new[]
                                {
                                    new OleDbParameter("@CoMemberId", OleDbType.Integer),
                                    new OleDbParameter("@CoIsActivation",OleDbType.Boolean),
                                    new OleDbParameter("@CoActivationId", OleDbType.VarChar, 100)
                                };
            parameter[0].Value = memberId;
            parameter[1].Value = true;
            parameter[2].Value = activationId;
            var count = DbHelperOleDb.ExecuteSql(sql.ToString(), parameter);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 锁定账号
        /// </summary>
        /// <param name="memberId">会员ID</param>
        /// <returns></returns>
        public bool Locking(string memberId)
        {
            var sbSql = new StringBuilder();
            sbSql.Append("update TbMember set CoIsLocking=@CoIsLocking  where CoMemberId=@CoMemberId;");
            var parameters = new[]
                                {
                                   new OleDbParameter("@CoIsLocking",OleDbType.Boolean),
                                   new OleDbParameter("@CoMemberId", OleDbType.Integer)
                                };
            parameters[1].Value = true;
            parameters[0].Value = memberId;
            var count = DbHelperOleDb.ExecuteSql(sbSql.ToString(), parameters);
            if (count > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 解锁账号
        /// </summary>
        /// <param name="memberId">会员ID</param>
        /// <returns></returns>
        public bool Unlock(string memberId)
        {
            var sbSql = new StringBuilder();
            sbSql.Append("update TbMember set CoIsLocking=@CoIsLocking  where CoMemberId=@CoMemberId;");
            var parameters = new[]
                                {
                                    new OleDbParameter("@CoIsLocking",OleDbType.Boolean),
                                    new OleDbParameter("@CoMemberId", OleDbType.Integer)
                                 
                                };
            parameters[0].Value = false;
            parameters[1].Value = memberId;
            var count = DbHelperOleDb.ExecuteSql(sbSql.ToString(), parameters);
            if (count > 0)
            {
                return true;
            }
            return false;
        }
        public static string GenerateActivationCode(string email)
        {
            if (!email.IsEmail())
            {
                return string.Empty;
            }
            string validataCode = FormsAuthentication.HashPasswordForStoringInConfigFile(Guid.NewGuid().ToString(), "md5");
            string to = email;
            const string subject = "账号激活";
            var body = new StringBuilder();
            body.Append("点击下面链接激活账号，48小时生效，否则重新注册账号，链接只能使用一次，请尽快激活！</br>");
            SendSmtpEMail(to, subject, body.ToString());
            return validataCode;
        }
        public static void SendSmtpEMail(string to, string subject, string body)
        {
            string smtpServer = ConfigService.SmtpServer;
            string from = ConfigService.From;
            string password = ConfigService.EmailPassword;
            var client = new System.Net.Mail.SmtpClient(smtpServer)
                             {
                                 UseDefaultCredentials = false,
                                 Credentials = new System.Net.NetworkCredential(from, password),
                                 DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
                             };
            var message = new System.Net.Mail.MailMessage(from, to, subject, body) { BodyEncoding = Encoding.UTF8, IsBodyHtml = true };
            client.Send(message);
        }
    }
}
